package com.atguigu.lease.common.mybatisplus;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * Package: com.atguigu.lease.common.mybatisplus
 *
 * @Description:
 * @Author xiongkunYang
 * @Create 2025/5/8 15:14
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class DatabaseWarmup implements CommandLineRunner {

    private final JdbcTemplate jdbcTemplate;

    @Override
    public void run(String... args) {
        try {
            Integer result = jdbcTemplate.queryForObject("SELECT 1", Integer.class);
            if (Integer.valueOf(1).equals(result)) {
                log.info("Database connection warmed up successfully.");
            }
        } catch (Exception e) {
            log.error("Failed to warm up database connection: {}", e.getMessage(), e);
        }
    }
}
